<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>每日签到</title>
    <base href="/weixin-qiandao/">
    <link href="./lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <script src="./lib/jquery/dist/jquery.min.js"></script>
    <script src="./lib/bootstrap/dist/js/bootstrap.min.js"></script>
    <style type="text/css">
        body {
            font-family: "Microsoft YaHei UI Light";
            font-size: 16px;
        }

        .brand {
            height: 200px;
            padding-top: 20px;
            background-image: url("./img/bg-01.jpg");
            background-size: 100% 200px;
        }

        .user-avatar {
            width: 80px;
            height: 80px;
            margin: auto;
            /*background-image: url("./img/avatar.jpg");*/
            background-size: 80px 80px;
            border-radius: 40px;
        }

        .btn-qiandao {
            height: 40px;
            line-height: 40px;
            margin: 15px 10px;
            text-align: center;
            color: #FFFFFF;
            background-color: #31A4E5;
            display: none;
        }

        .is-signed {
            height: 40px;
            line-height: 40px;
            margin: 15px 10px;
            text-align: center;
            color: #FFFFFF;
            background-color: #31A4E5;
            display: none;
        }
    </style>
</head>
<body>
<div class="brand">
    <div class="user-avatar"></div>
    <div style="margin: 35px 0 22px 16px;color: #FFFFFF;">
        <div>连续签到 :<span id="continuousNum" style="margin-left: 5px"></span></div>
        <div style="margin-top: 5px">签到次数 :<span id="totalCheckInNum" style="margin-left: 5px;"></span></div>
    </div>
</div>
<div class="btn-qiandao" onclick="qiandao()">点击签到</div>
<div class="is-signed">已签到</div>
<div style="margin: 16px 20px;color: #95989A;">每日仅可签到一次</div>

<script type="text/javascript">

    function qiandao() {
        if (busy) {
            console.log("busy...");
            return false;
        }
        $.get("qiandao/signIn", function (res) {
            if (res.id) {
                $(".btn-qiandao").css("display", "none");
                $(".is-signed").css("display", "block");
            }
            getTotalAndContinuousNum();
        })
                .fail(function (err) {
                    if (err.status === 401) {
                        location.href = "qiandao/"
                    } else {
                        console.log(err)
                    }
                })
    }

    function getTotalAndContinuousNum() {
        $.get("qiandao/getTotalAndContinuousNum", function (res) {
            console.log("res", res);
            $("#totalCheckInNum").html(res.totalCheckInNum);
            $("#continuousNum").html(res.continuousNum);
            if (res.signed) {
                $(".btn-qiandao").css("display", "none");
                $(".is-signed").css("display", "block");
            } else {
                $(".btn-qiandao").css("display", "block");
            }
            console.log("userInfo", res.userInfo);
            var userInfo = res.userInfo ? JSON.parse(res.userInfo) : null;
            if (userInfo) {
                $(".user-avatar").css("background-image", "url('" + userInfo.headimgurl.substr(0, userInfo.headimgurl.length - 1) + "96')")
            }
        })
                .fail(function (res) {
                    console.log("fail", res);
                    if (res.status === 401) {
                        location.href = "qiandao/"
                    }
                    else {
                        alert("status：" + res.status + ";message:" + res.message)
                    }
                })
    }

    var busy = false;
    getTotalAndContinuousNum();

</script>
</body>
</html>